<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_categories
 *
 * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('JPATH_BASE') or die;

JFormHelper::loadFieldClass('list');
require_once (JPATH_ADMINISTRATOR.'/components/com_k2store/library/popup.php');
/**
 * Form Field class for the Joomla Framework.
 *
 * @package     Joomla.Administrator
 * @subpackage  com_categories
 * @since       1.6
 */
class JFormFieldItemOptionsJ3 extends JFormFieldList
{
	/**
	 * A flexible attribute options
	 *
	 * @var		string
	 * @since	3.0
	 */
	public $type = 'ItemOptionsJ3';

	/**
	 * Method to get a list of attributes that and options
	 *
	 * @return	array	The field option objects.
	 * @since	3.0
	 */
	
	function getInput() {
		
		$fieldName = $this->fieldname;
		//get libraries
		$html ='';
		
		$cid = JRequest::getVar('cid');
		if($cid) {
			$link = 'index.php?option=com_k2store&view=products&task=setattributes&id='.$cid.'&tmpl=component';
			$import_link = 'index.php?option=com_k2store&view=products&task=setpaimport&id='.$cid.'&tmpl=component';
				
			//let us first get Product Attribute Names
			$attributes = $this->getProductAttributes($cid);
			if(!empty($attributes)) {
				$html .=$attributes;
			}
		
			$html .= K2StorePopup::popup( $link, JText::_( "K2STORE_ADD_EDIT_PRODUCT_ATTRIBUTES" ), array('onclose' => '\function(){k2storeNewModal(\''.JText::_('K2STORE_SAVING_CHANGES').'\'); Joomla.submitbutton(\'apply\');}'));
			$html .= JText::_('K2STORE_OR');
			//$html .= K2StorePopup::popup( $import_link, JText::_( "K2STORE_IMPORT_PRODUCT_ATTRIBUTES" ), array() );
			$html  .= '&nbsp;'.JText::_( "K2STORE_IMPORT_PRODUCT_ATTRIBUTES" ).'&nbsp;'.JText::_('K2STORE_AVAILABLE_IN_PRO_VERSION');
				
		} else {
			$html .='<p class="text-info">';
			$html .= JText::_('K2STORE_ITEM_OPTIONS_SAVE');
			$html .='</p>';
		}
		
		return $html;
		
	}
	
	function getProductAttributes($product_id) {
	
		$db = &JFactory::getDBO();
		$query = 'SELECT a.* FROM #__k2store_productattributes AS a WHERE a.product_id='. (int) $product_id
		.' ORDER BY a.ordering ASC'
		;
		$db->setQuery($query);
		$rows = $db->loadObjectList();
		$html= '';
	
		if(count($rows)) {
			$html .='<table class="adminlist table table-striped" >
			<thead>
			<th>'.JText::_('Attribute Name').'</th>
			<th>'.JText::_('Set options').'</th>
			<th>'.JText::_('Options set for this Attribute').'</th>
			<th>'.JText::_('Remove').'</th>
			</thead>
			<tbody>';
			foreach($rows as $row) {
				//lets get a list of attribute options for each attribute
				$pa_options = $this->getProductAttributeOptions($row->productattribute_id);
	
				$html .='<tr>';
				$html .='<td>'.$row->productattribute_name.'</td>';
				$html .='<td>';
				$html .= K2StorePopup::popup( "index.php?option=com_k2store&view=products&task=setattributeoptions&id=".$row->productattribute_id."&tmpl=component", JText::_( "Set options to this attribute" ), array());
				$html .='</td>';
				//attribute options
				$html .='<td>';
				if(!empty($pa_options)) {
					$html .=$pa_options;
				}
				$html .='</td>';
				$html .='<td>';
				$html .='<a href="index.php?option=com_k2store&view=products&task=deleteattributes&product_id='.$product_id.'&cid[]='.$row->productattribute_id.'&return='.base64_encode("index.php?option=com_k2&view=item&cid=".$product_id).'">';
				$html .= JText::_( 'Delete Attribute' );
				$html .='</a>';
				$html .='</td>';
				$html .='</tr>';
			}
				
			$html .='</tbody>';
			$html .='</table>';
		}
		return $html;
	}
	
	
	function getProductAttributeOptions($pa_id) {
	
		$db = &JFactory::getDBO();
		$query = 'SELECT a.* FROM #__k2store_productattributeoptions AS a WHERE a.productattribute_id='. (int) $pa_id
		.' ORDER BY a.ordering ASC'
		;
		$db->setQuery($query);		
		$rows = $db->loadObjectList();
	
		$html = '';
		foreach ($rows as $row) {
			$html .= $row->productattributeoption_name.',&nbsp;&nbsp;';
		}
	
		return $html;
	}
	
	
}